package org.ztest.thrift;

import java.io.IOException;

import org.apache.thrift.TException;
import org.apache.thrift.async.AsyncMethodCallback;
import org.apache.thrift.async.TAsyncClientManager;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TMultiplexedProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.transport.TNonblockingSocket;
import org.apache.thrift.transport.TNonblockingTransport;
import org.apache.thrift.transport.TTransport;
import org.ztest.thrift.echo.Echo;
import org.ztest.thrift.echo.Echo.AsyncClient.echo_call;
import org.ztest.thrift.tutorial.Calculator;
import org.ztest.thrift.tutorial.Calculator.AsyncClient.add_call;

// 非阻塞型客户端
public class JavaMultiplesClient
{
	public static void main(String[] args) throws TException, IOException, InterruptedException
	{
		TTransport transport = new TNonblockingSocket("localhost", 9090);
		
		Echo.AsyncClient eac = new Echo.AsyncClient(new Factory(), new TAsyncClientManager(), (TNonblockingTransport) transport);
		
		eac.echo("This is echo message", new AsyncMethodCallback<echo_call>() {

			@Override
			public void onError(Exception exception)
			{
				exception.printStackTrace();
			}

			@Override
			public void onComplete(echo_call response)
			{
				try {
					System.out.println(response.getResult());
				} catch (TException e) {
					e.printStackTrace();
				}
			}
		});
		
		TTransport transport2 = new TNonblockingSocket("localhost", 9090);
		Calculator.AsyncClient cac = new Calculator.AsyncClient(new Factory2(), new TAsyncClientManager(), (TNonblockingTransport) transport2);
		cac.add(1, 3, new AsyncMethodCallback<add_call>() {

			@Override
			public void onComplete(add_call response)
			{
				try {
					System.out.println(response.getResult());
				} catch (TException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onError(Exception exception)
			{
				exception.printStackTrace();
			}
		});
		
		// 异步方式需要延迟等待结果
		Thread.sleep(1000);
	}
	
	public static class Factory implements TProtocolFactory {
		private static final long serialVersionUID = 1L;

		public Factory() {
	    }

	    public TProtocol getProtocol(TTransport transport) {
//	    	TBinaryProtocol protocol = new TBinaryProtocol(transport);
	    	TProtocol protocol = new TCompactProtocol(transport);
			TMultiplexedProtocol tp = new TMultiplexedProtocol(protocol, "ECHO");
			return tp;
	    }
	  }
	
	public static class Factory2 implements TProtocolFactory {
		private static final long serialVersionUID = 1L;

		public Factory2() {
	    }

	    public TProtocol getProtocol(TTransport transport) {
//	    	TBinaryProtocol protocol = new TBinaryProtocol(transport);
	    	TProtocol protocol = new TCompactProtocol(transport);
			TMultiplexedProtocol tp = new TMultiplexedProtocol(protocol, "CALCULATOR");
			return tp;
	    }
	  }
}
